#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_task_base.py 
@version:
@time: 2022/03/11 
"""
from TapClientCore.T05_TM_feature.v2.TMF04_meta_transformer.tmf_meta_transformer import TMFMetaTransformerItems
from TapUtil.logfunc.logger import logger
from TapClientCore.T01_init_conf.v2_init.init_config import MFA, config, task_collection
from TapClientCore.T05_TM_feature.v2.TMF03_task.tmf_task import TMFTask
from TapException.TM import *
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm


class TPFTaskBase:
    TMF_TASK = TMFTask()
    TMF_META = TMFMetaTransformerItems()
    serve = config.get('serve')
    user = v2tm.user

    def task_insert_mongodb(self, kwargs):
        MFA.insert_one(task_collection,
                       {
                           'serve': self.serve,
                           'user': self.user,
                           **kwargs
                       })

    def task_delete_mongodb(self, kwargs):
        name = kwargs.get('name')
        MFA.delete_one(task_collection,
                       {
                           'name': name,
                           'user': self.user,
                           'serve': self.serve
                       })

    def task_update_mongodb(self, kwargs):
        name = kwargs.get('name')
        return MFA.update(task_collection,
                          {'name': name,
                           'serve': self.serve,
                           'user': self.user,
                           },
                          {'$set': {**kwargs}})

    def task_query_mongodb(self, kwargs):
        """
        通过名字查询任务
        :param kwargs:
        :return:
        """
        name = kwargs.get('name')
        query_result = MFA.query(task_collection,
                                 {
                                     'name': name,
                                     'serve': self.serve,
                                     'user': self.user,
                                 })
        if query_result:
            return query_result[0]
        return query_result

    async def delete_all_record_data_source(self):
        result = MFA.query(task_collection,
                           {
                               'serve': self.serve,
                               'user': self.user,
                           })
        for i in result:
            delete_response = None
            print(i.get('name'))
            try:
                if self.TMF_TASK.query_task_by_name(i.get('name')):
                    if self.TMF_TASK.query_task_by_name(i.get('name')).get('status') in ['complete', 'edit', 'error',
                                                                                    'stop']:
                        print('delete')
                        delete_response = self.TMF_TASK.manage_delete_task(i.get('name'))
                        MFA.delete_one(task_collection,
                                       {
                                           'serve': self.serve,
                                           'name': i.get('name'),
                                           'user': self.user,
                                       })
                else:
                    print('not found or can not delete')
            except DataFlowRemoveFail as e:
                logger.fatal(e)

    def delete_record_by_query(self, query):
        result = MFA.query(task_collection,
                           {
                               **query,
                               'serve': self.serve,
                               'user': self.user,
                           })
        for i in result:
            delete_response = None
            try:
                if self.TMF_TASK.query_task_by_name(i.get('name')):
                    if self.TMF_TASK.query_task_by_name(i.get('name')).get('status') in ['complete', 'edit', 'error',
                                                                                    'stop']:
                        delete_response = self.TMF_TASK.manage_delete_task(i.get('name'))
                        MFA.delete_one(task_collection,
                                       {
                                           'serve': self.serve,
                                           'name': i.get('name'),
                                           'user': self.user,
                                       })
                        logger.info(f'delete task {i.get("name")}')
            except DataFlowRemoveFail as e:
                logger.fatal(e)
